package com.onpositive.gamemaker.visualizer.swt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import com.onpositive.gamemaker.core.CompositeSprite;
import com.onpositive.gamemaker.core.GameLevel;
import com.onpositive.gamemaker.core.ISprite;
import com.onpositive.gamemaker.core.MapCell;
import com.onpositive.gamemaker.core.TerrainType;
import com.onpositive.gamemaker.objects.GameObjectType;
import com.onpositive.gamemaker.objects.IGameObject;
import com.onpositive.gamemaker.visualizer.ISpriteProvider;

public class BasicSpriteProvider implements ISpriteProvider
{
	protected GameLevel level;
	protected HashMap<TerrainType, ISprite> terrainSprites = new HashMap<TerrainType, ISprite>();
	protected HashMap<GameObjectType, ISprite> gameObjectSprites = new HashMap<GameObjectType, ISprite>();
//	Image img1 = new Image(Display.getDefault(), GCCreateFrom.class.getResourceAsStream("grass01.png"));
//	Image img2 = new Image(Display.getDefault(), GCCreateFrom.class.getResourceAsStream("grass02.png"));
//	Image img3 = new Image(Display.getDefault(), GCCreateFrom.class.getResourceAsStream("grass03.png"));
//	Image tree1;
//	Image tree2;

	public BasicSpriteProvider()
	{
//		ImageData ideaData = new ImageData(
//				GCCreateFrom.class.getResourceAsStream("tree01.png"));
//		int whitePixel = ideaData.palette.getPixel(new RGB(255,255,255));
//		ideaData.transparentPixel = whitePixel;
//		    Image transparentIdeaImage = new Image(Display.getDefault(),ideaData);
//		tree1 = transparentIdeaImage;
//		ideaData = new ImageData(
//				GCCreateFrom.class.getResourceAsStream("tree02.png"));
//		whitePixel = ideaData.palette.getPixel(new RGB(255,255,255));
//		ideaData.transparentPixel = whitePixel;
//		transparentIdeaImage = new Image(Display.getDefault(),ideaData);
//		tree2 = transparentIdeaImage;
	}

	@Override
	public CompositeSprite getSprites(int x, int y)
	{
		List<ISprite> result = new ArrayList<ISprite>();
		MapCell cell = level.getCell(x,y);
		result.add(getCellSprite(cell));
		IGameObject[] gameObjects = level.getGameObject(x,y);
		for (int i = 0; i < gameObjects.length; i++)
		{
			result.add(getGameObjectSprite(gameObjects[i]));
		}
		return new CompositeSprite(null, result.toArray(new ISprite[result.size()]));
	}

	protected ISprite getGameObjectSprite(IGameObject gameObject)
	{
		return gameObjectSprites.get(gameObject.getType());
	}

	protected ISprite getCellSprite(MapCell cell)
	{		
		return terrainSprites.get(cell.getTerrainType());
	}
	
	public void registerTerrainTypeSprite(TerrainType terrainType, ISprite sprite)
	{
		terrainSprites.put(terrainType, sprite);
	}
	
	public void registerGameObjectTypeSprite(GameObjectType gameObjectType, ISprite sprite)
	{
		gameObjectSprites.put(gameObjectType, sprite);
	}

	/**
	 * @return the level
	 */
	public GameLevel getLevel()
	{
		return level;
	}

	/**
	 * @param level the level to set
	 */
	public void setLevel(GameLevel level)
	{
		this.level = level;
	}

}
